<template>
	<view class="shopping-details-address">
		<u-popup :show="show" mode="bottom" :overlay="true" round="30rpx" @open="open">
			<view class="box">
				<view class="content">
					<view class="row-1">
						<!-- listData[selecotrIndex[0]._index].pic -->
						<image :src="realmName + listData[0].pic" mode="aspectFill"
							class="image" />
						<view class="info">
							<view class="price">
								<text style="font-size: 24rpx; padding-bottom: 10rpx;">￥</text>
								<text>{{newPrice}}</text>
							</view>
							<view class="tips">
								<text style="color: #878787;font-size: 24rpx;margin-right: 12rpx;">已选</text>
								<text>{{ruleTips}}</text>
							</view>
						</view>
						<view class="icon-close" @click="close">
							<u-icon name="close" size="12" color="#BDBDBD"></u-icon>
						</view>
					</view>
					<view class="row-2" v-for="(item,index) in listDataIsPost" :key="index">
						<view class="title">{{item.title}}</view>
						<view class="list">
							<!-- active empty -->
							<view class="list-item" v-for="(i,x) in item.ch" :key="x"
								:class="!i.isPost?'empty':JSON.stringify(selecotrIndex).indexOf(index+'_'+x) != -1?'active':''"
								@click="handleSelecotr(index,x,i)">
								{{i.name}}
							</view>
						</view>
					</view>
					<view class="row-3">
						<view class="title">数量</view>
						<view class="tool">
							<view class="tool-box" @click="handleNumb('-')"
								style="display: flex;justify-content: flex-end;">
								<u-icon name="minus" size="18" :color="input > 1 ? '#000000' : '#EEEEEE'"></u-icon>
							</view>
							<input type="number" v-model="input" class="input" @blur="handleEmit" disabled>
							<view class="tool-box" @click="handleNumb('+')">
								<u-icon name="plus" size="18" :color="addInputColor"></u-icon>
							</view>
						</view>
					</view>
					<view class="row-4"></view>
				</view>
				<view class="row-5">
					<view class="join" @click="handleAddShopping">加入购物车</view>
					<view class="buy" @click="handleGoBuy">立即购买</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		realmName,
	} from "@/http";
	import {
		addWcShoppingCart
	} from "@/culturalCreativity/http/shoppingCart.js";
	export default {
		props: {
			listDataEd: {
				type: Array,
				default: () => []
			},
			listData: {
				type: Array,
				default: () => []
			},

		},
		data() {
			return {
				show: false,
				val: false,
				input: 1,
				selecotrIndex: [],
				listDataIsPost: [],
				realmName
			}
		},
		methods: {
			init() {
				this.show = true;
				this.listDataIsPost = this.listData.map((item, index) => { //增加库存
					let options = {
						...item
					}
					options.ch = item.ch.map(i => {
						let obj = {}
						this.listDataEd.forEach(o => {
							if (item.title == o[`title${index+1}`] && i == o[
									`title${index+1}Val`]) {
								obj.isPost = o.ispost;
							};
						});
						obj.name = i;
						return obj;
					});
					return options;
				});
				if (!this.selecotrIndex.length) {
					this.listData.forEach((item, index) => {
						for (let i = 0; i < item.ch.length; i++) {
							this.selecotrIndex.push({
								index,
								_index: i,
								z: `${index}_${i}`
							});
							break;
						}
					})
				};
			},
			handleNumb(str) {
				if (str == '-') {
					if (Number(this.input) > 1) {
						this.input = Number(this.input) - 1;
					}
				} else {
					if (this.addInputColor == "#EEEEEE") return;
					this.input = Number(this.input) + 1;
				};
				this.handleEmit();
			},
			handleSelecotr(index, _index, item) {
				if (!item.isPost) return uni.showToast({
					title: "暂无库存",
					icon: "error"
				});
				if (this.selecotrIndex.some(item => item.index == index && item._index == _index)) {
					// this.selecotrIndex = this.selecotrIndex.filter(item => {
					// 	return !(item.index == index && item._index == _index);
					// })
				} else {
					this.selecotrIndex = this.selecotrIndex.filter(item => {
						return item.index != index
					});
					this.selecotrIndex.push({
						index,
						_index,
						z: `${index}_${_index}`
					});
				};
				// 排序
				this.selecotrIndex.sort((a, b) => {
					return a.index - b.index;
				});
				this.handleEmit();
			},
			handleEmit() {
				this.$emit("change", this.selecotrIndex);
				this.$emit("num", this.input);
				this.$emit("price", this.comPrice);
			},
			async handleAddShopping() {
				try{
					this.selecotrIndex.forEach(item => {
						if (this.listDataIsPost[item.index].ch[item._index].isPost < this.input) {
							uni.showToast({
								title: "库存不足",
								icon: "error"
							});
							throw ("库存不足");
						}
					});
					let pageData = uni.getStorageSync('PAGE_DATA');
					console.log('查看购物车数据：', pageData);
					let data = pageData.list[0];
					uni.showLoading({
						title: "",
						mask: true
					});
					let res = await addWcShoppingCart({
						...data,
						specificationPic: this.listData[0].pic, // this.listData[this.selecotrIndex[0]._index].pic
						goodsPic: this.listData[0].pic,
						specificationName: `${data.title1 || ''} ${data.title1Val || ''} ${data.title2 || ''} ${data.title2Val || ''}`
					});
					uni.showToast({
						title: "加入成功",
						icon: "success"
					});
				}catch(e){
					console.error(e)
					uni.hideLoading();
				}
			},
			handleGoBuy() {
				try {
					this.selecotrIndex.forEach(item => {
						if (this.listDataIsPost[item.index].ch[item._index].isPost < this.input) {
							uni.showToast({
								title: "库存不足",
								icon: "error"
							});
							throw ("库存不足");
						}
					});
					this.$emit("buy", this.listData[this.selecotrIndex[0].index].pic);
				} catch (e) {
					console.error(e);
				}
			},
			open() {
				// console.log('open');
			},
			close() {
				this.show = false;
			},
		},
		computed: {
			ruleTips() {
				let str = this.selecotrIndex.reduce((pev, item) => {
					pev = pev + this.listData[item.index].ch[item._index]
					return pev;
				}, '');
				if (!str) return ''
				return `${str} ${this.input}个`;
			},
			comPrice() {
				let obj = {}
				this.selecotrIndex.forEach(item => {
					obj[`title${item.index+1}`] = this.listData[item.index].title;
					obj[`title${item.index+1}Val`] = this.listData[item.index].ch[item._index];
				});
				return (this.listDataEd.reduce((pev, item) => {
					let lock = [...Object.keys(obj)].some(i => {
						return obj[i] != item[i];
					});
					if (!lock) {
						pev = pev + item.mealCardPay;
					}
					return pev;
				}, 0) * this.input).toFixed(2);
			},
			
			newPrice(){
				let obj = {}
				this.selecotrIndex.forEach(item => {
					obj[`title${item.index+1}`] = this.listData[item.index].title;
					obj[`title${item.index+1}Val`] = this.listData[item.index].ch[item._index];
				});
				return (this.listDataEd.reduce((pev, item) => {
					let lock = [...Object.keys(obj)].some(i => {
						return obj[i] != item[i];
					});
					if (!lock) {
						pev = pev + item.mealCardPay;
					}
					return pev;
				}, 0)).toFixed(2);
			},
			
			
			addInputColor() {
				let color = "#000000";
				this.selecotrIndex.forEach(item => {
					if (this.listDataIsPost[item.index].ch[item._index].isPost <= this.input) {
						color = "#EEEEEE"
					}
				});
				return color;
			}
		}
	}
</script>

<style lang="scss" scoped>
	.shopping-details-address {

		.box {
			padding-top: 32rpx;

			.content {
				padding: 0 32rpx;
				border-bottom: 1px solid #EEEEEE;
			}

			.title {
				font-size: 24rpx;
				font-weight: 500;
				color: #1A1A1A;
			}

			.row-5 {
				padding: 10rpx 32rpx calc(env(safe-area-inset-bottom) + 78rpx) 32rpx;
				display: flex;
				justify-content: space-between;

				.join,
				.buy {
					height: 76rpx;
					line-height: 76rpx;
					text-align: center;
					background: #FD8F19;
					border: 2rpx solid #FD8F19;
					border-radius: 40rpx;
					font-size: 32rpx;
					font-weight: 400;
					color: #FFFFFF;
				}

				.join {
					width: 196rpx;
					background-color: #FFFFFF;
					color: #FD8F19;
				}

				.buy {
					width: 458rpx;
				}
			}

			.row-4 {
				height: 101rpx;
			}

			.row-3 {
				display: flex;
				justify-content: space-between;
				margin-top: 72rpx;

				.tool {
					display: flex;
					align-items: center;

					.tool-box {
						width: 40rpx;
						position: relative;
						$size: -20rpx;
						&:nth-of-type(1)::after{
							position: absolute;
							content: "";
							left: $size;
							top: $size;
							bottom: $size;
							right: 0;
						}
						
						&:nth-of-type(2)::after{
							position: absolute;
							content: "";
							left: 0rpx;
							top: $size;
							bottom: $size;
							right: $size;
						}
					}

					.input {
						width: 66rpx;
						height: 34rpx;
						line-height: 34rpx;
						text-align: center;
						background: #F1F1F1;
						border-radius: 8rpx;
						font-size: 24rpx;
						font-weight: 500;
						color: #565656;
						margin: 0 12rpx;
					}
				}
			}

			.row-2 {
				margin-top: 32rpx;

				.list {
					margin-top: 40rpx;
					display: flex;
					flex-wrap: wrap;
					margin-left: -24rpx;

					.list-item {
						margin-left: 24rpx;
						min-width: 156rpx;
						padding: 0 12rpx;
						line-height: 56rpx;
						text-align: center;
						height: 56rpx;
						background: rgba(189, 189, 189, 0.2);
						border-radius: 30rpx;
						border: 2rpx solid #BDBDBD;
						font-size: 21rpx;
						font-weight: 500;
						color: #565656;
					}

					.list-item.empty {
						color: #C1C1C1;
						background-color: #EEEEEE;
						border-color: #EEEEEE;
					}

					.list-item.active {
						color: #FD8F19;
						background-color: rgba(253, 143, 25, 0.2);
						border-color: #FD8F19;
						;
					}
				}
			}

			.row-1 {
				position: relative;
				display: flex;
				align-items: flex-end;

				.info {

					.price,
					.tips {
						display: flex;
						align-items: flex-end;
					}

					.price {
						font-size: 48rpx;
						font-weight: 500;
						color: #FD8F19;
					}

					.tips {
						font-size: 24rpx;
						font-weight: 400;
						color: #1A1A1A;
						margin-top: 67rpx;
					}
				}

				.image {
					width: 208rpx;
					height: 208rpx;
					border-radius: 16rpx;
					margin-right: 24rpx;
				}

				.icon-close {
					position: absolute;
					right: 0;
					top: 0;
					background-color: #F1F1F1;
					height: 38rpx;
					width: 38rpx;
					border-radius: 50%;
					display: flex;
					align-items: center;
					justify-content: center;
				}
			}
		}
	}
</style>
